package sync

import (
	"gitee.com/hexug/go-tools/logger"
	"gitee.com/hexug/kube-sync/apps/cleandata"
	"gitee.com/hexug/kube-sync/apps/resource"
	appsv1 "k8s.io/api/apps/v1"
	"k8s.io/client-go/kubernetes"
	"os"
)

func (s *WorkloadSyncer) listDeployments(req *resource.ListDeployRequest) map[string][]*appsv1.Deployment {
	dps, err := s.dpsvc.ListDeploy(s.ctx, req)
	if err != nil {
		logger.L().Fatal(err)
	}
	ishas := false
	for ns, item := range dps {
		for _, dp := range item {
			ishas = true
			logger.L().Infow("找到Deployment", "namespace", ns, "resource", dp.Name)
		}
	}
	if !ishas {
		logger.L().Info("没有需要处理的资源")
		os.Exit(0)
	}
	return dps
}

func (s *WorkloadSyncer) cleanWorkloads(dps *resource.ListWorkloadRespone) *cleandata.CleanWorkloadResponse {
	// todo 未完成清洗其他资源
	res, err := s.clndsvc.CleanWorkload(s.ctx, dps)
	if err != nil {
		logger.L().Fatal(err)
	}
	return res
}

func (s *WorkloadSyncer) cleanServices(wks []interface{}, clientSet *kubernetes.Clientset) *cleandata.CleanServiceResponse {
	reqGetWks := resource.NewFromWorkloadGetResourceRequest()
	reqGetWks.ClientSet = clientSet
	reqGetWks.AddWorkload(wks...)
	svcs, err := s.dpsvc.FromWorkloadGetService(s.ctx, reqGetWks)
	if err != nil {
		logger.L().Fatal(err)
	}

	req := cleandata.NewCleanServiceRequest()
	req.AddService(svcs...)

	res, err := s.clndsvc.CleanService(s.ctx, req)
	if err != nil {
		logger.L().Fatal(err)
	}
	return res
}

func (s *WorkloadSyncer) cleanConfigMap(wks []interface{}, clientSet *kubernetes.Clientset) *cleandata.CleanConfigMapResponse {
	reqGetWks := resource.NewFromWorkloadGetResourceRequest()
	reqGetWks.ClientSet = clientSet
	reqGetWks.AddWorkload(wks...)
	cms, err := s.dpsvc.FromWorkloadGetConfigMaps(s.ctx, reqGetWks)
	if err != nil {
		logger.L().Fatal(err)
	}
	req := cleandata.NewCleanConfigMapRequest()
	req.AddConfigMap(cms...)
	res, err := s.clndsvc.CleanConfigMap(s.ctx, req)
	if err != nil {
		logger.L().Fatal(err)
	}
	return res
}

func (s *WorkloadSyncer) CleanRBAC(wks []interface{}, clientSet *kubernetes.Clientset) *cleandata.CleanRBACResponse {
	req := resource.NewFromWorkloadGetResourceRequest()
	req.ClientSet = clientSet
	req.AddWorkload(wks...)
	resRBAC, err := s.dpsvc.FromWorkloadGetRBAC(s.ctx, req)
	if err != nil {
		logger.L().Fatal(err)
	}
	res, err := s.clndsvc.CleanRBAC(s.ctx, resRBAC)
	if err != nil {
		logger.L().Fatal(err)
	}
	return res
}

func (s *WorkloadSyncer) CleanPvPvc(wks []interface{}, clientSet *kubernetes.Clientset) *cleandata.CleanPvPvcResponse {
	req := resource.NewFromWorkloadGetResourceRequest()
	req.ClientSet = clientSet
	req.AddWorkload(wks...)
	resPvPvc, err := s.dpsvc.FromWorkloadGetPVPVC(s.ctx, req)
	if err != nil {
		logger.L().Fatal(err)
	}
	res, err := s.clndsvc.CleanPvPvc(s.ctx, resPvPvc)
	if err != nil {
		logger.L().Fatal(err)
	}
	return res
}

func (s *WorkloadSyncer) CleanSecret(wks []interface{}, set *kubernetes.Clientset) *cleandata.CleanSecretResponse {
	req := resource.NewFromWorkloadGetResourceRequest()
	req.ClientSet = set
	req.AddWorkload(wks...)
	resSecret, err := s.dpsvc.FromWorkloadGetSecret(s.ctx, req)
	if err != nil {
		logger.L().Fatal(err)
	}
	res, err := s.clndsvc.CleanSecret(s.ctx, resSecret)
	if err != nil {
		logger.L().Fatal(err)
	}
	return res
}
